package com.sztle.admodule.apiprocessor;

import android.app.Activity;
import android.os.CountDownTimer;

import com.sztle.admodule.GmAdManager;
import com.sztle.admodule.IShowListener;
import com.sztle.admodule.bean.AdReportInfo;
import com.sztle.admodule.handler.FullIntlHandler;
import com.sztle.admodule.outstatus.AppStatusProcessor;
import com.sztle.admodule.control.event.SzAdEvents;
import com.sztle.admodule.control.manager.ControlManager;
import com.sztle.admodule.control.utils.UserDeviceUtils;

import java.util.Random;

public class OutIntAdApi {

    private static CountDownTimer timer;
    private static int lastIndex = -1;

    public static void showTwoIntAd(Activity activity, String sceneName) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        cancelAdTimer();
        ControlManager.triggerQueryAdjust();
        if(UserDeviceUtils.isGMore()){
            return;
        }
        if(!GmAdManager.isUseAdExp()){
            return;
        }
        if (!GmAdManager.isAdSdkReady()) {
            return;
        }
        if(!FullIntlHandler.canLoad()){
            return;
        }
        if(lastIndex == -1){
            lastIndex = new Random().nextInt(3);
        } else {
            lastIndex = ++lastIndex % 3;
        }
       final int curIndex = lastIndex;
        FullIntlHandler.loadAndShow(curIndex, activity, new IShowListener() {
            @Override
            public void onFinalEnd(boolean result) {
                if(!result){
                    return;
                }
                lastIndex = ++lastIndex % 3;
                int tempIndex = lastIndex;
                FullIntlHandler.loadAndShow(tempIndex, activity, new IShowListener() {
                    @Override
                    public void onAdShow(String ecmp) {
                        if(tempIndex == 0){
                            ControlManager.reportAdLog(SzAdEvents.show_int_1, new AdReportInfo(ecmp, sceneName));
                        } else if (tempIndex == 1) {
                            ControlManager.reportAdLog(SzAdEvents.show_int_2, new AdReportInfo(ecmp, sceneName));
                        } else {
                            ControlManager.reportAdLog(SzAdEvents.show_int_3, new AdReportInfo(ecmp, sceneName));
                        }
                    }

                    @Override
                    public void onFinalEnd(boolean result) {
                        if(result){
                            startAdTimer();
                        }
                    }
                });
            }

            @Override
            public void onAdShow(String ecmp) {
                if(curIndex == 0){
                    ControlManager.reportAdLog(SzAdEvents.show_int_1, new AdReportInfo(ecmp, sceneName));
                } else if (curIndex == 1) {
                    ControlManager.reportAdLog(SzAdEvents.show_int_2, new AdReportInfo(ecmp, sceneName));
                } else {
                    ControlManager.reportAdLog(SzAdEvents.show_int_3, new AdReportInfo(ecmp, sceneName));
                }
            }
        });


    }

    public static void showTwoIntAd(Activity activity, String sceneName,IShowListener showListener) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        cancelAdTimer();
        ControlManager.triggerQueryAdjust();
        if(UserDeviceUtils.isGMore()){
            if(showListener != null){
                showListener.onFinalEnd(false);
            }
            return;
        }
        if(!GmAdManager.isUseAdExp()){
            if(showListener != null){
                showListener.onFinalEnd(false);
            }
            return;
        }

        if (!GmAdManager.isAdSdkReady()) {
            if(showListener != null){
                showListener.onFinalEnd(false);
            }
            return;
        }
        if(!FullIntlHandler.canLoad()){
            if(showListener != null){
                showListener.onFinalEnd(false);
            }
            return;
        }
        if(lastIndex == -1){
            lastIndex = new Random().nextInt(3);
        } else {
            lastIndex = ++lastIndex % 3;
        }
        final int curIndex = lastIndex;
        FullIntlHandler.loadAndShow(curIndex,activity, new IShowListener() {

            @Override
            public void onFinalEnd(boolean result) {
                if(!result){
                    //失败不请求
                    if(showListener != null){
                        showListener.onFinalEnd(false);
                    }
                    return;
                }
                lastIndex = ++lastIndex % 3;
                final int tempIndex = lastIndex;
                FullIntlHandler.loadAndShow(tempIndex,activity, new IShowListener() {

                    @Override
                    public void onFinalEnd(boolean result) {
                        if(showListener != null){
                            showListener.onFinalEnd(result);
                        }
                        if(result){
                            startAdTimer();
                        }

                    }

                    @Override
                    public void onAdShow(String ecmp) {
                        if(tempIndex == 0){
                            ControlManager.reportAdLog(SzAdEvents.show_int_1, new AdReportInfo(ecmp, sceneName));
                        } else if (tempIndex == 1) {
                            ControlManager.reportAdLog(SzAdEvents.show_int_2, new AdReportInfo(ecmp, sceneName));
                        } else {
                            ControlManager.reportAdLog(SzAdEvents.show_int_3, new AdReportInfo(ecmp, sceneName));
                        }
                    }
                });

            }

            @Override
            public void onAdShow(String ecmp) {
                if(curIndex == 0){
                    ControlManager.reportAdLog(SzAdEvents.show_int_1, new AdReportInfo(ecmp, sceneName));
                } else if (curIndex == 1) {
                    ControlManager.reportAdLog(SzAdEvents.show_int_2, new AdReportInfo(ecmp, sceneName));
                } else {
                    ControlManager.reportAdLog(SzAdEvents.show_int_3, new AdReportInfo(ecmp, sceneName));
                }
            }

        });

    }

    //单次广告请求加载
    public static void showSingleIntAd(Activity activity, String sceneName) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        cancelAdTimer();
        ControlManager.triggerQueryAdjust();
        if(UserDeviceUtils.isGMore()){
            return;
        }
        if(!GmAdManager.isUseAdExp()){
            return;
        }
        if (!GmAdManager.isAdSdkReady()) {
            return;
        }
        if(!FullIntlHandler.canLoad()){
            return;
        }
        if(lastIndex == -1){
            lastIndex = new Random().nextInt(3);
        } else {
            lastIndex = ++lastIndex % 3;
        }
        FullIntlHandler.loadAndShow(lastIndex, activity, new IShowListener() {
            @Override
            public void onAdShow(String ecmp) {
                if(lastIndex == 0){
                    ControlManager.reportAdLog(SzAdEvents.show_int_1, new AdReportInfo(ecmp, sceneName));
                } else if (lastIndex == 1) {
                    ControlManager.reportAdLog(SzAdEvents.show_int_2, new AdReportInfo(ecmp, sceneName));
                } else {
                    ControlManager.reportAdLog(SzAdEvents.show_int_3, new AdReportInfo(ecmp, sceneName));
                }

            }

            @Override
            public void onFinalEnd(boolean result) {
                if(result){
                    startAdTimer();
                }
            }

        });

    }

    public static void showSingleIntAd(Activity activity, String sceneName,IShowListener showListener) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        cancelAdTimer();
        ControlManager.triggerQueryAdjust();
        if(UserDeviceUtils.isGMore()){
            if(showListener != null){
                showListener.onFinalEnd(false);
            }
            return;
        }
        if(!GmAdManager.isUseAdExp()){
            if(showListener != null){
                showListener.onFinalEnd(false);
            }
            return;
        }
        if (!GmAdManager.isAdSdkReady()) {
            if(showListener != null){
                showListener.onFinalEnd(false);
            }
            return;
        }
        if(!FullIntlHandler.canLoad()){
            if(showListener != null){
                showListener.onFinalEnd(false);
            }
            return;
        }

        if(lastIndex == -1){
            lastIndex = new Random().nextInt(3);
        } else {
            lastIndex = ++lastIndex % 3;
        }

        FullIntlHandler.loadAndShow(lastIndex,activity, new IShowListener() {
            @Override
            public void onAdShow(String ecmp) {
                if(lastIndex == 0){
                    ControlManager.reportAdLog(SzAdEvents.show_int_1, new AdReportInfo(ecmp, sceneName));
                } else if (lastIndex == 1) {
                    ControlManager.reportAdLog(SzAdEvents.show_int_2, new AdReportInfo(ecmp, sceneName));
                } else {
                    ControlManager.reportAdLog(SzAdEvents.show_int_3, new AdReportInfo(ecmp, sceneName));
                }

            }

            @Override
            public void onFinalEnd(boolean result) {
                if(showListener != null){
                    showListener.onFinalEnd(result);
                }
                if(result){
                    startAdTimer();
                }
            }

        });


    }


    private static void showThirdIntAd(Activity activity, String sceneName, IShowListener listener) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        lastIndex = 2;
        FullIntlHandler.loadAndShow(2,activity, new IShowListener() {
            @Override
            public void onFinalEnd(boolean result) {
                if (listener != null) {
                    listener.onFinalEnd(result);
                }
            }

            @Override
            public void onAdShow(String ecmp) {
                ControlManager.reportAdLog(SzAdEvents.show_int_3, new AdReportInfo(ecmp, sceneName));
            }

        });
    }

    private static void showSecondIntAd(Activity activity, String sceneName, IShowListener listener) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        lastIndex = 1;
        FullIntlHandler.loadAndShow(1,activity, new IShowListener() {
            @Override
            public void onFinalEnd(boolean result) {
                if (listener != null) {
                    listener.onFinalEnd(result);
                }
            }

            @Override
            public void onAdShow(String ecmp) {
                ControlManager.reportAdLog(SzAdEvents.show_int_2, new AdReportInfo(ecmp, sceneName));
            }

        });
    }

    private static void showFirstIntAd(Activity activity, String sceneName, IShowListener listener) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        lastIndex = 0;
        FullIntlHandler.loadAndShow(0,activity, new IShowListener() {

            @Override
            public void onFinalEnd(boolean result) {
                if(listener != null){
                    listener.onFinalEnd(result);
                }
            }

            @Override
            public void onAdShow(String ecmp) {
                ControlManager.reportAdLog(SzAdEvents.show_int_1, new AdReportInfo(ecmp, sceneName));
            }

        });
    }

    private static void showFirstIntAd(Activity activity, String sceneName) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        lastIndex = 0;
        FullIntlHandler.loadAndShow(0,activity, new IShowListener() {


            @Override
            public void onAdShow(String ecmp) {
                ControlManager.reportAdLog(SzAdEvents.show_int_1, new AdReportInfo(ecmp, sceneName));
            }

        });
    }

    private static void showSecondIntAd(Activity activity, String sceneName) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        lastIndex = 1;
        FullIntlHandler.loadAndShow(1,activity, new IShowListener() {
            @Override
            public void onAdShow(String ecmp) {
                ControlManager.reportAdLog(SzAdEvents.show_int_2, new AdReportInfo(ecmp, sceneName));
            }

        });
    }

    private static void showThirdIntAd(Activity activity, String sceneName) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        lastIndex = 2;
        FullIntlHandler.loadAndShow(2,activity, new IShowListener() {

            @Override
            public void onAdShow(String ecmp) {
                ControlManager.reportAdLog(SzAdEvents.show_int_3, new AdReportInfo(ecmp, sceneName));
            }

        });
    }

    public static void startAdTimer() {
        long time = ControlManager.getTimerInterval();
        if(time <= 0){
            return;
        }
        if (timer == null) {
            timer = new CountDownTimer(time * 1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {

                }

                @Override
                public void onFinish() {
                    if (AppStatusProcessor.canPopTimerAd()) {
                        showSingleIntAd(AppStatusProcessor.getTopActivity(), "timerAd");
                    }
                }
            };
            timer.start();
        }
    }

    public static void cancelAdTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    public static void clearFlag(){
        FullIntlHandler.setClearFlag();
    }
}
